import * as shopService from '../../services/shop/shop';
import * as systemService from '../../services/system';
import { message } from 'antd';
export default {
  namespace: 'charge',
  state:{
    customer:undefined,
    payCustomer:undefined,
    admins:[],
  },
  reducers: {
    updateCustomer(state, {payload}) {
      const newstate = state;
      newstate.customer = payload;
      return newstate;
    },
    updatePayCustomer(state, {payload}) {
      const newstate = state;
      newstate.payCustomer = payload;
      return newstate;
    },
    updateAdmins(state, {payload}) {
      const newstate = state;
      newstate.admins = payload;
      return newstate;
    },
  },
  effects: {
    *e_getCustomer({payload}, {call, put}) {
      const {data} = yield call(shopService.getCustomer,payload);
      yield put({type: 'updateCustomer', payload: data});
    },
    *e_getPayCustomer({payload}, {call, put}) {
      const {data} = yield call(shopService.getCustomer,payload);
      yield put({type: 'updatePayCustomer', payload: data});
    },
    *e_charge({payload}, {call, put}) {
      const {data} = yield call(shopService.charge,payload);
      if(data.id===payload.customerId){
        message.success(<span style={{color:'green',marginLeft:5,fontSize:18}}> 充值成功,到账 :{payload.amount} 元</span>);
      }
      yield put({type: 'updateCustomer', payload: undefined});
    },
    *e_pay({payload}, {call, put}) {
      const {data} = yield call(shopService.payCustomer,payload);
      if(data==null){
        message.success(<span style={{color:'green',marginLeft:5,fontSize:18}}> 匿名付款成功</span>);
      }else if(data.id===payload.customerId){
        message.success(<span style={{color:'green',marginLeft:5,fontSize:18}}> 会员付款成功</span>);
      }
      yield put({type: 'updatePayCustomer', payload: undefined});
    },
    *e_outPay({payload}, {call, put}) {
      const {data} = yield call(shopService.outPay,payload);
      if(data==null){
        message.success(<span style={{color:'green',marginLeft:5,fontSize:18}}>商户出款成功</span>);
      }
    },
    *e_getMyAdmins({payload}, {call, put}) {
      const {data} = yield call(systemService.getMyAdmins,payload);
      yield put({type: 'updateAdmins', payload: data});
    },
  },

}
